<?php
Class Controller_Manage_Supplier extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建供应商';
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建供应商保存';
		$suppliernumber = isset($_POST['suppliernumber'])?$_POST['suppliernumber']:'';
		$suppliername = isset($_POST['suppliername'])?$_POST['suppliername']:'';
		$regnum = isset($_POST['regnum'])?$_POST['regnum']:'';
		$recorddate = isset($_POST['recorddate'])?$_POST['recorddate']:'';
		$corporate = isset($_POST['corporate'])?$_POST['corporate']:'';
		$phone = isset($_POST['phone'])?$_POST['phone']:'';
		$time = date('Y-m-d H:i:s');
		
		if(strlen($phone)<11)
		{
			return $this->_redirectMessage('提交失败', "请填写正确的手机号码", url('Manage::Supplier/Create'),FAILURE);
		}
		
		if($suppliernumber && $suppliername)
		{
			$exsit = VegeSupplier::find('suppliernumber=? or suppliername=?',$suppliernumber,$suppliername)->asArray()->getOne();
			if(isset($exsit['id'])&&$exsit['id'])
			{
				return $this->_redirectMessage('提交失败', "对应名称或编号的供应商信息已经存在", url('Manage::Supplier/Create'),FAILURE);
			}
			
			$new = new VegeSupplier();
			$new->suppliernumber = $suppliernumber;
			$new->suppliername = $suppliername;
			$new->regnum = $regnum;
			$new->recorddate = $recorddate;
			$new->corporate = $corporate;
			$new->phone = $phone;
			$new->time = $time;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Supplier/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::Supplier/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '供应商信息列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeSupplier::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Supplier/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '供应商信息详情';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$product = VegeSupplier::find('id=?', $id)->asArray()->getOne();
		if ($product)
		{
			$this->_view['product'] =  $product;
		}
		else
		{
			return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Supplier/List'), FAILURE);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '供应商编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegesupplier = VegeSupplier::find('id=?', $id)->asArray()->getOne();
		if ($vegesupplier)
		{
			$this->_view['vegesupplier'] = $vegesupplier;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '供应商不存在', url('Manage::Supplier/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '供应商编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$suppliernumber = isset($_POST['suppliernumber'])?$_POST['suppliernumber']:'';
			$suppliername = isset($_POST['suppliername'])?$_POST['suppliername']:'';
			$regnum = isset($_POST['regnum'])?$_POST['regnum']:'';
			$recorddate = isset($_POST['recorddate'])?$_POST['recorddate']:'';
			$corporate = isset($_POST['corporate'])?$_POST['corporate']:'';
			$phone = isset($_POST['phone'])?$_POST['phone']:'';
			$time = date('Y-m-d H:i:s');
			
			if(strlen($phone)<11)
			{
				return $this->_redirectMessage('提交失败', "请填写正确的手机号码", url('Manage::Supplier/Edit'),FAILURE);
			}
			
			if($suppliernumber && $suppliername && $id)
			{
				$edit = VegeSupplier::find('id=?', $id)->getOne();
				$edit->suppliernumber = $suppliernumber;
				$edit->suppliername = $suppliername;
				$edit->regnum = $regnum;
				$edit->recorddate = $recorddate;
				$edit->corporate = $corporate;
				$edit->phone = $phone;
				$edit->time = $time;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Supplier/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$result = VegeSupplier::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegeSupplier::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionExportBusinessBaseInfo()
	{
		$this->_view['title'] = '导出供应商信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$vegesupplier = VegeSupplier::find()->asArray()->getAll();
		//dump($businessbaseinfo);exit;
		if ($vegesupplier)
		{
			$baseinfo = BaseNodeInfo::find()->asArray()->getOne();
			
			$filename = $baseinfo['companynumber'] . '_KHDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			foreach($vegesupplier as $vs)
			{
				$line .= ",".$vs['suppliernumber'];
				$line .= ",".$vs['suppliername'];
				$line .= ",".$vs['regnum'];
				$line .= ",".str_replace('-','',$vs['recorddate']);
				$line .= ",".$vs['corporate'];
				$line .= ",".$vs['phone'];
				$line .= ",".str_replace('-','',$vs['updatetime']);
				fputs($out, $line."\r\n");
			}
			
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('导出失败', '没有任何信息', url('Manage::Supplier/List'), FAILURE);
		}
	}
}